import { obj_int, obj_empty } from 'sui';
import { do_event } from './event';
import { put_log } from 'base/ctl/log';
import { step_code } from 'base/step';
import { clear_step } from 'db/state';
import { loc } from 'local/index.js';

const do_ready = (umd, watch) => {
    clear_step();

    umd.set_stepnum(1);

    do_event("step_start", umd, watch);
};

const do_start = (stepnum, umd, watch) => {
    put_log("工步号", stepnum, "工步开始");

    const num = stepnum - 1;

    const cur = umd.curStep(num);
    const code = obj_int(cur, "stepcode");

    put_log("操作代码", code, "工步开始");

    if (!cur || !code) {
        umd.setErr(loc("step_69"));
        return;
    }

    const stcode = step_code[code];

    if (obj_empty(stcode)) {
        umd.setErr(`${loc("step_70")} ${code}`);
        return;
    }

    const st = stcode.st;

    do_event(st, umd);
    watch.startWatch();
};

const do_over = (stepnum, umd, watch) => {
    const next = stepnum + 1;
    if (next > umd.stepLength()) {
        return;
    }

    umd.set_stepnum(next);

    do_event("step_start", umd, watch);
};

export {do_start, do_over, do_ready};
